package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    // 1.查询所有部门信息
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    // 2.根据id删除部门信息
    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    //3.新增部门
    @Override
    public void save(Dept dept) {
        //补全基础属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //保存部门
        deptMapper.Insert(dept);

    }

    //4.根据id查询部门
    @Override
    public Dept findById(Integer id) {
        return deptMapper.findById(id);

    }

    //5.再根据id修改部门
    @Override
    public void update(Dept dept) {
        //补全基础属性
        dept.setUpdateTime(LocalDateTime.now());

        //修改部门
        deptMapper.update(dept);
    }

    // 检查部门下是否有员工
    @Override
    public boolean hasEmployees(Integer deptId) {
        return empMapper.countByDeptId(deptId) > 0;
    }
}
